package ycl.security.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;
import ycl.common.constants.SecurityConstant;
import ycl.security.service.LoginService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 退出登录
 *
 * @author ycl
 * @date 2022-03-10 13:00:30
 */
@Slf4j
@Component
public class AuthLogoutHandler implements LogoutHandler {
	@Resource
	private LoginService loginService;

	@Override
	public void logout(HttpServletRequest request,
					   HttpServletResponse response,
					   Authentication authentication) {
		String token = request.getHeader(SecurityConstant.TOKEN);
		String loginType = request.getHeader(SecurityConstant.LOGIN_TYPE);
		String remoteAddr = request.getRemoteAddr();
		loginService.logout(token, loginType, remoteAddr);
	}
}
